//
//  AppDelegate.swift
//  JourneyThroughTime
//
//  Created by iOS-Czz on 2023/11/6.
//

import UIKit
import IQKeyboardManagerSwift
import WXNavigationBar

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        let isFirst = UserDefaults.standard.bool(forKey: "isFirst")
        if isFirst == false{
            self.configDefaultData()
        }
        
        IQKeyboardManager.shared.enable = true
        WXNavigationBar.setup()
        let rootViewController = AJTTTabBarController()
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = .white
        window?.rootViewController = rootViewController
        window?.makeKeyAndVisible()
        return true
    }
    
    func configDefaultData(){
        UserDefaults.standard.setValue(true, forKey: "isFirst")
        do {
            let path = Bundle.main.path(forResource: "AJTTItemData", ofType: "json")
            let jsonData = try Data(contentsOf: URL(fileURLWithPath: path!), options: .mappedIfSafe)
            let jsonString = String(data: jsonData, encoding: .utf8)
            let helper = AJTTItemModel.getUsingLKDBHelper()
            helper.getTableCreated(with: AJTTItemModel.self)
            let data = jsonString!.data(using: String.Encoding.utf8)
            if let jsonData = data {
                let decoder = JSONDecoder()
                let persons = try! decoder.decode([AJTTItemModel].self, from: jsonData)
                for index in 0...persons.count-1 {
                    let model = persons[index]
                    if index == 0 {
                        if let path = Bundle.main.path(forResource: "detaultTelevision", ofType: "txt") {
                            if let readContent = String.read(fromFilePath: path) {
                                model.evolution = readContent
                            }
                        }
                    }else{
                        if let path = Bundle.main.path(forResource: "detaultTelephone", ofType: "txt") {
                            if let readContent = String.read(fromFilePath: path) {
                                model.evolution = readContent
                            }
                        }
                    }
                    model.saveToDB()
                }
            }
        } catch {
            print("Failed to read JSON file: \(error.localizedDescription)")
        }
        
        do {
            let path = Bundle.main.path(forResource: "AJTTStoryData", ofType: "json")
            let jsonData = try Data(contentsOf: URL(fileURLWithPath: path!), options: .mappedIfSafe)
            let jsonString = String(data: jsonData, encoding: .utf8)
            let helper = AJTTStoryModel.getUsingLKDBHelper()
            helper.getTableCreated(with: AJTTStoryModel.self)
            let data = jsonString!.data(using: String.Encoding.utf8)
            if let jsonData = data {
                let decoder = JSONDecoder()
                let persons = try! decoder.decode([AJTTStoryModel].self, from: jsonData)
                for index in 0...persons.count-1 {
                    let model = persons[index]
                    model.saveToDB()
                }
            }
        } catch {
            print("Failed to read JSON file: \(error.localizedDescription)")
        }
        
        do {
            let path = Bundle.main.path(forResource: "AJTTMineData", ofType: "json")
            let jsonData = try Data(contentsOf: URL(fileURLWithPath: path!), options: .mappedIfSafe)
            let jsonString = String(data: jsonData, encoding: .utf8)
            let helper = AJTTMineModel.getUsingLKDBHelper()
            helper.getTableCreated(with: AJTTMineModel.self)
            let data = jsonString!.data(using: String.Encoding.utf8)
            if let jsonData = data {
                let decoder = JSONDecoder()
                let persons = try! decoder.decode([AJTTMineModel].self, from: jsonData)
                for index in 0...persons.count-1 {
                    let model = persons[index]
                    model.saveToDB()
                }
            }
        } catch {
            print("Failed to read JSON file: \(error.localizedDescription)")
        }
        
       
    }
    
}

